/**
 * Project: ld.biz.service
 * 
 * File Created at 2012-9-23
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.biz.core.autocode;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liangdev.common.util.VelocityUtil;

/**
 * TODO Comment of TemplateRenderer
 * 
 * @author chongan.wangca
 */
public class TemplateRenderer {

    private final Logger logger = LoggerFactory.getLogger(TemplateRenderer.class);

    /**
     * 渲染模板，并返回渲染后的结果
     * 
     * @param templateFile
     * @param contextMap
     * @return
     */
    public String render(File templateFile, Map<String, Object> contextMap) {
        Reader reader;
        try {
            reader = new FileReader(templateFile);
        } catch (FileNotFoundException e) {
            logger.error("", e);
            return null;
        }
        Writer writer = VelocityUtil.getInstance().evaluateToWriter(contextMap, reader);
        return writer.toString();
    }

}
